/**
 * Created by gongxun on 17/1/13.
 */

var PdkTableLayer = GameCCSBase.extend({
    mPdkTableCardLayer: null,
    mPdkTableUILayer: null,
    mPdkPlayEffectLayer : null,
    mKwxApplyTableLayer: null,
    mPdkTableRoomEndLayer: null,
    mPdkTableReplayLayer: null,

    //牌桌状态 0录像 1正常打牌
    mPlayerType: null,

    //统计打了多少局
    mPlayGameCount: null,

    //游戏规则
    mGameConfig: null,

    //房间号
    mGroupId: null,

    //群号
    mGroupName: null,

    //录像号
    mReplayId: null,

    //密码
    mTableEnterPwd: null,

    //是否是创建者
    mIsRoomCreator: null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/PdkTable.json");
        mo.pdkTable = this;
        this.mIsRoomCreator = false;
        mo.pdkCardHelper.init();

        this.loadUI();
    },

    onEnter: function () {
        this._super();
        this.runAction(cc.sequence(cc.delayTime(1), cc.callFunc(function () {
            mo.soundHelper.playMusicGamePdk();
        })));

        mo.eventHelper.add("PdkForceExitTableResponsepdkOK", this, function () {
            if (mo.pdkTable.mGameConfig.maxpan == mo.pdkTableStatus.mTableStatus.LeftPlayTimes) {
                mo.showMsgDialogOk("退出牌局成功", function () {
                    this.quitPdkGame();
                }, this);
            }
        });

        mo.eventHelper.add("CancleJuRequestpdk", this, function (cancleJu) {
            this.mKwxApplyTableLayer.updateCancelJuInfo(cancleJu);
        });

        //TODO
        // this.loadPlayCard();
        // var inhandCardss = [
        //     mo.pdkCardHelper.getRandomCard(16),
        //     mo.pdkCardHelper.getRandomCard(16),
        //     mo.pdkCardHelper.getRandomCard(16)
        // ];
        // this.setFapaiAnim(inhandCardss);
        // this.mPdkTableUILayer.setPlayerTime(0, 1);
        //
        // var dealCardss = [
        //     mo.pdkCardHelper.getRandomCard(14),
        //     mo.pdkCardHelper.getRandomCard(12),
        //     mo.pdkCardHelper.getRandomCard(13)
        // ];
        // this.setChupai(dealCardss);
    },

    onExit: function () {
        mo.eventHelper.remove(this);
        this._super();
    },

    //正常打牌
    loadPlayCard: function () {
        this.mPlayGameCount = 0;
        this.mPlayerType = 1;
        this.mPdkTableUILayer.intoShowIndex(10);

        //隐藏录像界面
        this.mPdkTableReplayLayer.setVisible(false);

        //隐藏录像时候的ui
        this.mPdkTableUILayer.setIsReplay(false);
    },

    //播放录像
    loadReplay: function (rePlaypdk, replayId) {
        this.mPlayGameCount = 0;
        this.mPlayerType = 0;
        this.mPdkTableUILayer.intoShowIndex(10);

        //录像规则
        mo.pdkTable.setPdkConfig(rePlaypdk.Result.CfgJson, "", "");

        //打开录像界面 并且播放
        this.mPdkTableReplayLayer.setVisible(true);
        this.mPdkTableReplayLayer.setReplayId(replayId);
        this.mPdkTableReplayLayer.onStart();

        //隐藏录像时候的ui
        this.mPdkTableUILayer.setIsReplay(true);

        this.mReplayId = replayId;
        mo.pdkTableStatus.setReplayMsg(rePlaypdk);
        mo.pdkTableStatus.playReplay();
    },

    //显示代开房人
    setShowRoomCreate: function (user) {
        if (user != null) {
            var kwxDaikaifangWidget = new KwxDaikaifangWidget();
            kwxDaikaifangWidget.setUserInfo(user);
            this.addChild(kwxDaikaifangWidget);
            kwxDaikaifangWidget.setPosition(cc.p(mo.gameSize.x - 250, mo.gameSize.y - 10));
        }
    },

    //处理货币结算
    onHandlerTableResult : function(){
        mo.pdkTableStatus.handlerTableResult();
    },

    //播放分数动画
    setResultScore : function(scores){
        this.mPdkTableUILayer.mPdkTableResultLayer.setResultScore(scores);
    },

    loadUI: function () {
        //sprite
        cc.spriteFrameCache.addSpriteFrames("res/pokerCards.plist", "res/pokerCards.png");
        cc.spriteFrameCache.addSpriteFrames("res/emojis.plist", "res/emojis.png");
        cc.spriteFrameCache.addSpriteFrames("res/emojis_index.plist", "res/emojis_index.png");

        //牌层
        this.mPdkTableCardLayer = new PdkTableCardLayer();
        this.addChild(this.mPdkTableCardLayer);

        //ui层
        this.mPdkTableUILayer = new PdkTableUILayer();
        this.addChild(this.mPdkTableUILayer);

        //动画层
        this.mPdkPlayEffectLayer = new PdkPlayEffectLayer();
        this.addChild(this.mPdkPlayEffectLayer);

        //取消
        this.mKwxApplyTableLayer = new KwxApplyTableLayer("pdk");
        this.addChild(this.mKwxApplyTableLayer);

        //录像层
        this.mPdkTableReplayLayer = new PdkTableReplayLayer();
        this.addChild(this.mPdkTableReplayLayer);
    },

    setPdkConfig: function (Cfgjson, tablePassWord, GroupName) {
        this.mGroupName = GroupName;
        this.mTableEnterPwd = tablePassWord;
        this.mGameConfig = JSON.parse(Cfgjson);
    },

    quitPdkGame: function () {
        if (mo.gameHelper.developerVersion == "2.0") {
            mo.sceneHelper.runSceneHall();
        } else if (mo.gameHelper.developerVersion == "3.0") {
            mo.sceneHelper.exitGameLayer();
            if (this.mGroupId != null) {
                mo.hallLayer.intoGroup(this.mGroupId);
            }
        }
    },

    //发牌
    setFapaiAnim: function (cardNumss) {
        this.mPdkTableCardLayer.setFapaiAnim(cardNumss);
        this.mPdkTableUILayer.setFapaiAnim(cardNumss);
    },

    //玩家出牌 []要不起 有值就是出的牌 null什么都不显示 LastDealSeat:上次操作的玩家
    setChupai: function (dealCardNumss, LastDealSeat) {
        this.mPdkTableCardLayer.setChupai(dealCardNumss, LastDealSeat);
    },

    //玩家打出手牌
    setDealCardNums: function (dealCardNums, dealSeat) {
        this.mPdkTableCardLayer.setDealCardNums(dealCardNums, dealSeat);
    },

    //小结算 翻开手牌 飘金币
    openAllPlayerCards: function (handCardNumss, scores, winSeat) {
        //录像会用到
        this.mPdkTableUILayer.Panel_caozuo.setVisible(false);
        this.mPdkTableUILayer.Image_clock.setVisible(false);

        this.scheduleOnce(function () {
            this.mPdkTableCardLayer.openInhandCards(handCardNumss);
            this.mPdkTableUILayer.setScoreAnim(scores, winSeat);
        }, 3);
    },

    //大结算
    onShowJuEndedLayer: function (juEndedEventpdk) {
        //判断是否需要直接退出 如果剩余局数和设置里面的局数一致 就直接退出
        var ruleJson = JSON.parse(juEndedEventpdk.CfgJson);

        if (mo.pdkTableStatus.mTableStatus.LeftPlayTimes == ruleJson.maxpan) {
            mo.showMsgDialogOk("牌局已经解散", function () {
                this.quitPdkGame();
            }, this);
        } else {
            this.mPdkTableRoomEndLayer = new PdkTableRoomEndLayer();
            mo.sceneHelper.showEndInfoLayer(this.mPdkTableRoomEndLayer);
            // this.addChild(this.mPdkTableRoomEndLayer);
            this.mPdkTableRoomEndLayer.setRoomEndInfo(juEndedEventpdk);

            if (juEndedEventpdk.EndFlag != 1) {
                //自然结束 通过当局结算打开
                this.mPdkTableRoomEndLayer.setVisible(false);
            } else {
                this.mPdkTableRoomEndLayer.setVisible(true);
            }
        }
    },

    //更新剩余牌局
    updateLastPlayTimes: function () {
        //更新规则
        var time = mo.pdkTableStatus.mTableStatus.LeftPlayTimes;
        if (time != null) {
            time = this.mGameConfig.maxpan - time + 1;
            if (mo.pdkTableStatus.mTableStatus.Step == 0) {
                time -= 1;
            }
            mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_roomId"), "房号:" + mo.pdkTableStatus.mTableStatus.TableID);

            mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_leftJu"), "局数:" + time + "/" + this.mGameConfig.maxpan);

            // mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_basePoint"), "基础底分:" + this.mGameConfig.basepoint);

            var hosttype = this.mGameConfig.hosttype == 0 ? "红桃三先出" : "赢家先出";
            mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_hostType"), hosttype);

            var alltop = this.mGameConfig.alltop == 0 ? "不封顶" : "8张封顶";
            mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_allTop"), alltop);

            var ruleText = "";
            var ruletriple = ["三带一", "三带二"];
            var ruleairplane = ["单翼", "双翼"];
            var rulefour = ["四带三 ", "不能四带三"];
            ruleText += ruletriple[this.mGameConfig.ruletriple] + ",";
            ruleText += ruleairplane[this.mGameConfig.ruleairplane] + ",";
            ruleText += rulefour[this.mGameConfig.rulefour];
            mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_ruleTriple"), ruleText);
        }
    },

    //更新当前底分
    updateCurrentBasePoint: function () {
        var Text_currentPoint = mo.seekWidgetByName(this.ccsNode, "Text_currentPoint");
    },

    //邀请玩家
    onInvite: function () {
        var gameName = "pdk";
        var configSummary = mo.gameHelper.getRuleConfigSummary(this.mGameConfig, gameName);
        configSummary += "，就等你来了(点击加入)";
        var tableId = mo.pdkTableStatus.mTableStatus.TableID;

        var title = "桌号:[" + tableId + "]" + "-" + mo.gameHelper.projectName + "-跑得快";
        var description = configSummary;
        mo.gameHelper.onWxShareWeb(title, description, mo.gameUserInfo.getExternalUrl(1, tableId));
    },

    //分享录像
    onShareReplay: function () {
        var gameName = "pdk";
        var configSummary = mo.gameHelper.getRuleConfigSummary(this.mGameConfig, gameName);
        configSummary += "，在游戏中输入录像id就能看到我的精彩回放了";

        var title = "录像id:" + this.mReplayId.substr(7) + "-" + mo.gameHelper.projectName + "-跑得快";
        var description = configSummary;

        mo.gameHelper.onWxShareWeb(title, description, mo.gameUserInfo.getExternalUrl(2, this.mReplayId.substr(7)));
    }
});